/* javascript for templates/views_bug_admin/list.html */

/* 初始化显示所有bug 的datatable */
function init_datatable(search_keys, search_vals)
{
    search_keys = search_keys || new Array();
    search_vals = search_vals || new Array();
    
    $('#dt-bugs').dataTable({
        "bProcessing": false,
        "bSort": false,
        // "bFilter": true, // search 框, 默认 true
        // "bLengthChange": true, //修改每页显示的个数, 默认 true
        "sAjaxSource": '/admin/get-bugs',
        "sServerMethod": "POST",
        "bDestroy":true,        // 防止重新 init 时出错
        "fnServerParams": function ( aoData ) {
            // alert(search_keys.length);
            for (var i=0; i<search_keys.length; i++)
                // alert(search_keys[i]);
                aoData.push( { "name": search_keys[i], "value": search_vals[i] } );
        },
    });
}

/* 检索按钮的事件 */
function search_bugs()
{
    var search_keys = new Array();
    var search_vals = new Array();

    var bug_id = $("#txt-bug-id").val();
    if (!isNaN(bug_id) && bug_id.length > 0) {       // 是数字
        search_keys.push("bug_id");
        search_vals.push(bug_id);
    }

    var bug_title = $("#txt-bug-title").val();
    if (bug_title.length > 0) {
        search_keys.push("bug_title");
        search_vals.push(bug_title);
    }

    var bug_importance = $("#sel-importance").val();
    if (bug_importance != "0") {
        search_keys.push("bug_importance");
        search_vals.push(bug_importance);
    }

    var bug_state = $("#sel-state").val();
    if (bug_state != "0") {
        search_keys.push("bug_state");
        search_vals.push(bug_state);
    }

    var bug_project = $("#sel-project").val();
    if (bug_project != "0") {
        search_keys.push("bug_project");
        search_vals.push(bug_project);
    }

    var bug_startdate = change_date_format($("#start-date").val());
    if (bug_startdate.length > 0) {
        search_keys.push("bug_startdate");
        search_vals.push(bug_startdate);
    }
    
    var bug_enddate = change_date_format($("#end-date").val());
    if (bug_enddate.length > 0) {
        search_keys.push("bug_enddate");
        search_vals.push(bug_enddate);
    }
    
    init_datatable(search_keys, search_vals);
}

/* 修改日期格式, 为了后台的python的处理 */
function change_date_format(orgianl_date)
{
    new_date = orgianl_date;
    new_date = new_date.replace(/年/, '-');
    new_date = new_date.replace(/月/, '-');
    new_date = new_date.replace(/日/, '');
    return new_date;
}
